package com.orangelife.mobile.property.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.util.DecimalUtil;
import com.curry.android.util.MoneyUtil;
import com.curry.android.util.ToastHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.property.biz.PropertyBiz;
import com.orangelife.mobile.widget.ScrollListView;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 账单明细的适配器
 *
 * @author wgf
 * @since v3.6.5
 */
public class PropertyBillFeeDetailMonthAdapter<T> extends FatherAdapter<T> {
    private Context mContext;
    private List<Map<String, Object>> mList;
    private PropertyBillFeeDetailChildAdapter itemAdapter;
    private boolean canOperate;
    private String selectedMonth;

    public PropertyBillFeeDetailMonthAdapter(Context mContext, List<Map<String, Object>> list, TextView tvAllCheck, boolean canOperate, String selectedMonth) {
        this.mContext = mContext;
        this.mList = list;
        this.selectedMonth = selectedMonth;
        initCheckStatus();
        this.canOperate = canOperate;
    }

    @Override
    public BaseAdapter setList(List<T> list) {
        return null;
    }

    @Override
    public int getCount() {
        return mList == null ? 0 : mList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_property_bill_detail_month, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Map<String, Object> map = mList.get(position);
        holder.tvCostName.setText(String.valueOf(map.get("month")));
        holder.tvCostContent.setText(MoneyUtil.handleMoney(String.valueOf(map.get("mtotal"))) + "元");
        updateCheckStatus(holder.ivCheck, String.valueOf(map.get("hasCheck")));
        boolean isDropDown = "true".equals(String.valueOf(map.get("showChild")));
        showChildItem(holder.lvItem, isDropDown);
        holder.ivDown.setTag(R.id.ivDown, position);

        holder.llCheck.setTag(R.id.ivCheck, position);
        holder.llCheck.setOnClickListener(mListener);
        holder.ivDown.setOnClickListener(mListener);
        if (isDropDown) {
            List<Map<String, Object>> itemlist = (List<Map<String, Object>>) map.get("feeDetails");
            if (itemlist != null) {
                itemAdapter = new PropertyBillFeeDetailChildAdapter(mContext, itemlist);
                holder.lvItem.setAdapter(itemAdapter);
            }
        }
        return convertView;
    }

    private void initCheckStatus() {//增加没有逗号的判断
        String[] ss = null;
        if (selectedMonth.contains(",")) {
            ss = selectedMonth.split(",");
        } else if (selectedMonth.length() > 0) {
            ss = new String[1];
            ss[0] = selectedMonth;
        }
        for (int i = 0; i < mList.size(); i++) {
            String month = (String) mList.get(i).get("month");
            boolean hasCheck = false;
            if (ss != null) {
                for (int j = 0; j < ss.length; j++) {
                    String selectedMonth = ss[j];
                    if (selectedMonth.equals(month)) {
                        hasCheck = true;
                        break;
                    }
                }
            }
            mList.get(i).put("hasCheck", hasCheck);
            mList.get(i).put("showChild", false);
        }
    }

    private void updateCheckStatus(ImageView iv, String hasChecked) {
        if ("true".equals(hasChecked)) {
            iv.setBackgroundResource(R.drawable.icon_selected);
        } else {
            iv.setBackgroundResource(R.drawable.icon_unselected);
        }
    }

    private void showChildItem(ScrollListView lvItem, boolean isDropDown) {
        if (isDropDown) {
            lvItem.setVisibility(View.VISIBLE);
        } else {
            lvItem.setVisibility(View.GONE);
        }
    }

    private void checkItem(String hasChecked, int pos) {
        String status = "true";
        if ("true".equals(hasChecked)) {
            status = "false";
        }
        for (int i = 0; i < mList.size(); i++) {
            if (status.equals("true")) {
                if (i <= pos) {
                    mList.get(i).put("hasCheck", true);
                } else {
                    mList.get(i).put("hasCheck", false);
                }
            } else {
                if (i < pos) {
                    mList.get(i).put("hasCheck", true);
                } else {
                    mList.get(i).put("hasCheck", false);
                }
            }
        }
    }

    private void dropDownChildItem(String isDropDown, int pos) {
        if ("true".equals(isDropDown)) {
            mList.get(pos).put("showChild", false);
        } else {
            mList.get(pos).put("showChild", true);
        }
        notifyDataSetChanged();
    }

    public String getSelectedMonth() {
        StringBuilder sb = new StringBuilder();
        boolean hasNoSelectedAll = true;
        boolean hasSelectedAll = true;
        String firstMonth = "";
        String tailMonth = "";
        for (int i = 0; i < mList.size(); i++) {
            if ((boolean) mList.get(i).get("hasCheck")) {//已选中
                hasNoSelectedAll = false;
                String month = (String) mList.get(i).get("month");
                sb.append(month);
                if (i == 0) {
                    firstMonth = month;
                } else {
                    tailMonth = month;
                }
                sb.append(",");
                ytotal = DecimalUtil.add(ytotal, (String) mList.get(i).get("mtotal"));
            } else {
                hasSelectedAll = false;
            }
        }

        yearSelectedInfo = PropertyBiz.getSelectedYearInfo(firstMonth, tailMonth);

        if (hasNoSelectedAll) {
            yearStatus = "2";
        } else if (hasSelectedAll) {
            yearStatus = "1";
        } else {
            yearStatus = "3";
        }
        String selectedMonth = sb.toString();
        if (selectedMonth.length() == 0) {
            selectedMonth = "";
        } else {
            selectedMonth = selectedMonth.substring(0, sb.length() - 1);
        }
        return selectedMonth;
    }


    private String yearSelectedInfo = "";

    private String ytotal = "0";

    private String yearStatus;

    public String getYearSelectedInfo() {
        return yearSelectedInfo;
    }

    public String getYtotal() {
        return ytotal;
    }

    public String getYearStatus() {
        return yearStatus;
    }

    public void selectAll() {
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).put("hasCheck", true);
        }
        notifyDataSetChanged();
    }

    public LinkedList<String> getFeeDetailIDs() {
        LinkedList<String> list = new LinkedList<>();
        for (int i = 0; i < mList.size(); i++) {
            String hasChecked = String.valueOf(mList.get(i).get("hasCheck"));
            if ("true".equals(hasChecked)) {
                List<String> ids = (List<String>) mList.get(i).get("feeDetailIDs");
                for (int j = 0; j < ids.size(); j++) {
                    String id = ids.get(j);
                    list.add(id);
                }
            }
        }
        return list;
    }

    private View.OnClickListener mListener = new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.llCheck:
                    if (canOperate) {
                        int pos = (int) v.getTag(R.id.ivCheck);
                        String hasChecked = String.valueOf(mList.get(pos).get("hasCheck"));
                        checkItem(hasChecked, pos);
                        notifyDataSetChanged();
                    } else {
                        ToastHelper.getInstance()._toast("对不起，请先处理其他年份！");
                    }
                    break;
                case R.id.ivDown:
                    int pos2 = (int) v.getTag(R.id.ivDown);
                    String isDropDown = String.valueOf(mList.get(pos2).get("showChild"));
                    dropDownChildItem(isDropDown, pos2);
                    notifyDataSetChanged();
                    break;
            }

        }
    };

    static class ViewHolder {
        @Bind(R.id.ivCheck)
        ImageView ivCheck;
        @Bind(R.id.ivDown)
        ImageView ivDown;
        @Bind(R.id.tvCostName)
        TextView tvCostName;
        @Bind(R.id.tvCostContent)
        TextView tvCostContent;
        @Bind(R.id.lvItem)
        ScrollListView lvItem;
        @Bind(R.id.rlItem)
        LinearLayout rlItem;
        @Bind(R.id.llCheck)
        LinearLayout llCheck;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
